package com.gzhryc.net_marketing.cloud.micro.api.controllers.v1;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.api.DefaultAPIResponse;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.business.dao.db.PayGoodsOrder;
import com.gzhryc.net_marketing.cloud.business.dao.db.TeamPayApply;
import com.gzhryc.net_marketing.cloud.business.dao.db.TeamPayApplyMember;
import com.gzhryc.net_marketing.cloud.business.dao.enums.PayGoodsOrderEnum;
import com.gzhryc.net_marketing.cloud.business.dao.enums.TeamPayApplyMemberEnum;
import com.gzhryc.net_marketing.cloud.business.dao.models.TeamPayApplyMemberStateCount;
import com.gzhryc.net_marketing.cloud.business.dao.models.TeamPayOrderStateCount;
import com.gzhryc.net_marketing.cloud.business.services.PayGoodsOrderService;
import com.gzhryc.net_marketing.cloud.business.services.TeamPayApplyMemberService;
import com.gzhryc.net_marketing.cloud.business.services.TeamPayApplyService;
import com.gzhryc.net_marketing.cloud.business.services.dto.PayGoodsOrderSearch;
import com.gzhryc.net_marketing.cloud.business.services.dto.TeamPayApplyMemberSearch;
import com.gzhryc.net_marketing.cloud.goods.dao.db.Goods;
import com.gzhryc.net_marketing.cloud.goods.dao.enums.GoodsEnum;
import com.gzhryc.net_marketing.cloud.goods.services.GoodsService;
import com.gzhryc.net_marketing.cloud.micro.api.ClientAPICodeConstants;
import com.gzhryc.net_marketing.cloud.micro.api.MicroConstants;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.DefaultResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderCloseRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderCountResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderInfoRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderInfoResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderListRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderListResponse;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderReceiveGoodsRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderUpdateAddressRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderWaitPayInfoRequest;
import com.gzhryc.net_marketing.cloud.micro.api.dto.v1.order.MemberOrderWaitPayInfoResponse;
import com.gzhryc.net_marketing.core.enums.GoodsOrderEnum;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.member.LoginMember;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberAddress;
import com.gzhryc.shared_device.oem.code.member.services.MemberAddressService;

public class MarketOrderController {
	
	@Route(value = { "/member/order/list.data" })
    public MemberOrderListResponse orderList(HttpServletRequest request) {
        MemberOrderListResponse apiResp = new MemberOrderListResponse(ClientAPICodeConstants.NULL_DATA);
        MemberOrderListRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderListRequest.class);
        if (apiReq != null && apiReq.getBody().getState() != null) {
        	LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (loginMember != null) {
                PayGoodsOrderSearch search = new PayGoodsOrderSearch();
                search.setMemberId(loginMember.getId());
                if (apiReq.getBody().getState() == 1) { //等待支付
                    search.setState(GoodsOrderEnum.State.Wait.index());
                } else if (apiReq.getBody().getState() == 2) {//等待发货
                    search.setState(GoodsOrderEnum.State.WaitDeliver.index());
                } else if (apiReq.getBody().getState() == 3) {//等待收货
                    search.setState(GoodsOrderEnum.State.WaitReceive.index());
                } else if (apiReq.getBody().getState() == 4) { //等待评价
                    search.setState(GoodsOrderEnum.State.WaitEvaluate.index());
                }

                List<PayGoodsOrder> orderList = PayGoodsOrderService.self().findBySearch(search, apiReq.getBody().getPageNo(),
                        apiReq.getBody().getPageLimit());
                if (orderList != null && orderList.size() > 0) {
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS);
                    for (PayGoodsOrder order : orderList) {
                        MemberOrderListResponse.Item item = new MemberOrderListResponse.Item();
                        item.setTradeNo(order.getTradeNo());
                        item.setGoodsId(order.getGoodsId());
                        item.setGoodsName(order.getGoodsName());
                        item.setGoodsTypeNames(order.getTypeNames());
                        item.setGoodsSpaceName(order.getGoodsSpaceName());

                        item.setGoodsImg(order.getGoodsImg());
                        if (StringUtils.isNotBlank(order.getGoodsImg())) {
                            if (!order.getGoodsImg().startsWith("http")) {
                        		String resDomain = PropertyTools.get().getString("marketResDomain");
                                item.setGoodsImg(resDomain + order.getGoodsImg());
                            }
                        }
                        item.setGoodsMoney(order.getGoodsMoneyStr());
                        item.setPayMoney(order.getPayMoneyStr());
                        item.setLogisticsMoney(order.getLogisticsMoneyStr());
                        item.setState(order.getState());
                        item.setStateName(GoodsOrderEnum.State.note(order.getState()));
                        item.setGoodsCount(order.getGoodsCount());
                        item.setCreateDate(DateFormatUtils.format(order.getCreateDate(), "yyyy-MM-dd HH:mm"));
                        item.setApplyId(order.getApplyId());
                        apiResp.getBody().getItems().add(item);
                    }
                }
            }
        }
        return apiResp;
    }
	
	@Route(value = { "/member/order/count.data" })
    public MemberOrderCountResponse orderCount(HttpServletRequest request){
        MemberOrderCountResponse apiResp = new MemberOrderCountResponse(ClientAPICodeConstants.SUCCESS);
        LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
        if (loginMember != null) {
            List<TeamPayOrderStateCount> orderStateCountList = PayGoodsOrderService.self().findStateCountByMemberId(loginMember.getId());
            if(orderStateCountList != null && orderStateCountList.size() > 0){
                for(TeamPayOrderStateCount stateCount : orderStateCountList){
                    if(GoodsOrderEnum.State.Wait.index().equals(stateCount.getState())){
                        if(stateCount.getTotal() != null){
                            apiResp.getBody().setWaitPayCount(stateCount.getTotal().intValue());
                        }
                    }else if(GoodsOrderEnum.State.WaitDeliver.index().equals(stateCount.getState())){
                        if(stateCount.getTotal() != null){
                            apiResp.getBody().setWaitDeliverCount(stateCount.getTotal().intValue());
                        }
                    }else if(GoodsOrderEnum.State.WaitReceive.index().equals(stateCount.getState())){
                        if(stateCount.getTotal() != null){
                            apiResp.getBody().setWaitReceiptCount(stateCount.getTotal().intValue());
                        }
                    }else if(GoodsOrderEnum.State.WaitEvaluate.index().equals(stateCount.getState())){
                        if(stateCount.getTotal() != null){
                            apiResp.getBody().setWaitEvaluationCount(stateCount.getTotal().intValue());
                        }
                    }
                }
            }

            List<TeamPayApplyMemberStateCount> stateCountList = TeamPayApplyMemberService.self().findStateCountByMemberId(loginMember.getId());
            if(stateCountList != null && stateCountList.size() > 0){
                for(TeamPayApplyMemberStateCount stateCount : stateCountList){
                    if(TeamPayApplyMemberEnum.State.Waiting.index().equals(stateCount.getState())){
                        if(stateCount.getTotal() != null){
                            apiResp.getBody().setTeamPayWaitPayCount(stateCount.getTotal().intValue());
                        }
                    }else if(TeamPayApplyMemberEnum.State.Participating.index().equals(stateCount.getState())){
                        if(stateCount.getTotal() != null){
                            apiResp.getBody().setTeamPayExecuteCount(stateCount.getTotal().intValue());
                        }
                    }
                }
            }
            TeamPayApplyMemberSearch search = new TeamPayApplyMemberSearch();
            search.setMemberId(loginMember.getId());
            search.setState(TeamPayApplyMemberEnum.State.Participating.index());
            List<TeamPayApplyMember> data = TeamPayApplyMemberService.self().findBySearch(search,1,1);
            if(data != null && data.size() > 0){
                apiResp.getBody().setTeamPayOrders(new ArrayList<>());
                List<Long> applyIds = new ArrayList<>();
                for(TeamPayApplyMember applyMember : data){
                    applyIds.add(applyMember.getApplyId());
                }

                List<TeamPayApply> payApplyList = TeamPayApplyService.self().findByIds(applyIds);
                if(payApplyList != null && payApplyList.size() > 0){
                    for(TeamPayApply payApply : payApplyList){
                        MemberOrderCountResponse.TeamPayOrder teamPayOrder = new MemberOrderCountResponse.TeamPayOrder();
                        teamPayOrder.setApplyId(payApply.getId());
                        teamPayOrder.setGoodsId(payApply.getGoodsId());
                        teamPayOrder.setGoodsName(payApply.getGoodsName());
                        teamPayOrder.setGoodsImg(payApply.getGoodsImg());
                        teamPayOrder.setPayMoney(payApply.getGoodsMoneyStr());
                        teamPayOrder.setTeamMemberTotal(payApply.getTeamMemberTotal());
                        teamPayOrder.setTeamMemberCount(payApply.getTeamMemberCount());
                        teamPayOrder.setTeamPayMoney(payApply.getTeamPayMoneyStr());
                        teamPayOrder.setEndDate(DateFormatUtils.format(payApply.getEndDate(),"yyyy-MM-dd HH:mm:ss"));

                        apiResp.getBody().getTeamPayOrders().add(teamPayOrder);
                    }
                }

                data = TeamPayApplyMemberService.self().findByStateAndApplyIds(TeamPayApplyMemberEnum.State.Participating.index(),applyIds);
                if(data != null && data.size() > 0){
                    for(TeamPayApplyMember applyMember : data){
                        for(MemberOrderCountResponse.TeamPayOrder order : apiResp.getBody().getTeamPayOrders()){
                            if(order.getApplyId().equals(applyMember.getApplyId())){
                                MemberOrderCountResponse.MemberItem memberItem = new MemberOrderCountResponse.MemberItem();
                                memberItem.setMemberId(applyMember.getMemberId());
                                memberItem.setFaceImg(applyMember.getMemberFaceImg());
                                memberItem.setNickname(applyMember.getMemberNickname());
                                memberItem.setType(applyMember.getRoleType());
                                order.getMembers().add(memberItem);
                                break;
                            }
                        }
                    }
                }
            }
        }
        return apiResp;
    }
	
	@Route(value = { "/member/order/info.data" })
    public MemberOrderInfoResponse orderInfo(HttpServletRequest request) {
        MemberOrderInfoResponse apiResp = new MemberOrderInfoResponse(ClientAPICodeConstants.EXECUTE_ERROR, "获取订单失败，请稍后再试");
        MemberOrderInfoRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderInfoRequest.class);
        if (apiReq != null && StringUtils.isNotBlank(apiReq.getBody().getTradeNo())) {
        	LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (loginMember != null) {
                PayGoodsOrder order = PayGoodsOrderService.self().getByTradeNo(apiReq.getBody().getTradeNo(), loginMember.getId());
                if (order != null) {
                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setTradeNo(order.getTradeNo());
                    apiResp.getBody().setBusinessType(order.getBusinessType());
                    apiResp.getBody().setBusinessTypeName(order.getBusinessTypeName());
                    apiResp.getBody().setGoodsId(order.getGoodsId());
                    apiResp.getBody().setGoodsName(order.getGoodsName());
                    apiResp.getBody().setGoodsSpaceName(order.getGoodsSpaceName());

                    apiResp.getBody().setGoodsImg(order.getGoodsImg());
                    if (StringUtils.isNotBlank(order.getGoodsImg())) {
                        if (!order.getGoodsImg().startsWith("http")) {
                    		String resDomain = PropertyTools.get().getString("marketResDomain");
                            apiResp.getBody().setGoodsImg(resDomain + order.getGoodsImg());
                        }
                    }
                    apiResp.getBody().setGoodsMoney(order.getGoodsMoneyStr());
                    apiResp.getBody().setMoney(order.getMoneyStr());
                    apiResp.getBody().setPayMoney(order.getPayMoneyStr());
                    apiResp.getBody().setLogisticsMoney(order.getLogisticsMoneyStr());
                    apiResp.getBody().setCouponMoney(order.getCouponMoneyStr());
                    apiResp.getBody().setGoodsCount(order.getGoodsCount());
                    apiResp.getBody().setSupplyState(order.getSupplyState());
                    apiResp.getBody().setState(order.getState());
                    apiResp.getBody().setStateName(order.getStateName());
                    apiResp.getBody().setPayType(order.getPayType());
                    apiResp.getBody().setPayTypeName(order.getPayTypeName());
                    apiResp.getBody().setCreateDate(DateFormatUtils.format(order.getCreateDate(), "yyyy-MM-dd HH:mm"));
                    // 团拼信息
                    TeamPayApply applyEntity = TeamPayApplyService.self().getById(order.getApplyId());
                    if (applyEntity != null) {
                        MemberOrderInfoResponse.TeamPayInfo teamPayInfo = new MemberOrderInfoResponse.TeamPayInfo();
                        teamPayInfo.setApplyId(applyEntity.getId());
                        teamPayInfo.setTeamMemberTotal(applyEntity.getTeamMemberTotal());
                        teamPayInfo.setState(applyEntity.getState());

                        List<TeamPayApplyMember> applyMemberList = TeamPayApplyMemberService.self().findAttendByApplyId(order.getApplyId());
                        if (applyMemberList != null && applyMemberList.size() > 0) {
                            for (TeamPayApplyMember applyMember : applyMemberList) {
                                MemberOrderInfoResponse.TeamPayInfoMember item = new MemberOrderInfoResponse.TeamPayInfoMember();
                                item.setMemberId(applyMember.getMemberId());
                                item.setNickname(applyMember.getMemberNickname());
                                item.setFaceImg(applyMember.getMemberFaceImg());
                                if (applyMember.getMemberId().equals(applyEntity.getMemberId())) {
                                    item.setType(1);
                                } else {
                                    item.setType(2);
                                }
                                teamPayInfo.getMembers().add(item);
                            }
                        }
                        apiResp.getBody().setTeamPayInfo(teamPayInfo);
                    }
                    // 收货地址
                    if (order.getMailAddressId() != null) {
                        MemberOrderInfoResponse.MailAddress mailAddress = new MemberOrderInfoResponse.MailAddress();
                        mailAddress.setId(order.getMailAddressId());
                        mailAddress.setReceiveName(order.getReceiveName());
                        mailAddress.setReceivePhone(order.getReceivePhone());
                        mailAddress.setReceiveAddress(order.getReceiveAddress());
                        apiResp.getBody().setMailAddress(mailAddress);
                    }
                }
            }
        }
        return apiResp;
    }
	
	/**
     * 获取等待支付的订单
     * @param request
     * @return
     */
    @Route(value = { "/member/order/waitPay.data" })
    public MemberOrderWaitPayInfoResponse waitPayOrderInfo(HttpServletRequest request) {
        MemberOrderWaitPayInfoResponse apiResp = new MemberOrderWaitPayInfoResponse(ClientAPICodeConstants.EXECUTE_ERROR, "获取订单失败，请稍后再试");
        MemberOrderWaitPayInfoRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderWaitPayInfoRequest.class);
        if (apiReq != null && StringUtils.isNotBlank(apiReq.getBody().getTradeNo())) {
        	LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (loginMember != null) {
                PayGoodsOrder order = PayGoodsOrderService.self().getByTradeNo(apiReq.getBody().getTradeNo(), loginMember.getId());
                if (order != null && GoodsOrderEnum.State.Wait.index().equals(order.getState())) {
                    Goods goods = GoodsService.self().getById(order.getGoodsId());

                    if (goods == null || GoodsEnum.State.OffTheShelf.index().equals(goods.getState())) {
                        //TODO 关闭支付
                        apiResp.setCode(ClientAPICodeConstants.EXECUTE_ERROR, "商品已下架");
                        return apiResp;
                    } else if (GoodsEnum.State.NoInventory.index().equals(goods.getState())) {
                        //TODO 关闭支付
                        apiResp.setCode(ClientAPICodeConstants.EXECUTE_ERROR, "商品暂无库存");
                        return apiResp;
                    }

                    MemberOrderWaitPayInfoResponse.GoodsEntity goodsEntity = new MemberOrderWaitPayInfoResponse.GoodsEntity();
                    goodsEntity.setGoodsId(goods.getId());
                    goodsEntity.setGoodsName(goods.getName());
                    goodsEntity.setThumbnailImg(goods.getThumbnailImg());
                    if (StringUtils.isNotBlank(goods.getThumbnailImg())) {
                        if (!goods.getThumbnailImg().startsWith("http")) {
                    		String resDomain = PropertyTools.get().getString("marketResDomain");
                            goodsEntity.setThumbnailImg(resDomain + goods.getThumbnailImg());
                        }
                    }
                    goodsEntity.setPayMoney(goods.getPayMoneyStr());
                    goodsEntity.setTeamPayMoney(goods.getTeamPayMoneyStr());
                    goodsEntity.setSupplierId(goods.getSupplierId());

                    MemberOrderWaitPayInfoResponse.MailAddress addressInfo = new MemberOrderWaitPayInfoResponse.MailAddress();
                    addressInfo.setId(order.getMailAddressId());
                    addressInfo.setReceiveName(order.getReceiveName());
                    addressInfo.setReceivePhone(order.getReceivePhone());
                    addressInfo.setAddress(order.getReceiveAddress());

                    apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                    apiResp.getBody().setTradeNo(order.getTradeNo());
                    apiResp.getBody().setBusinessType(order.getBusinessType());
                    apiResp.getBody().setGoodsNum(order.getGoodsCount());
                    apiResp.getBody().setSpecificationId(order.getGoodsSpaceId());
                    apiResp.getBody().setSpecificationName(order.getGoodsSpaceName());
                    apiResp.getBody().setAddressInfo(addressInfo);
                    apiResp.getBody().setGoods(goodsEntity);
                    apiResp.getBody().setLogisticsCompanyId(order.getLogisticsCompanyId());
                } else {
                    TeamPayApplyMember applyMember = TeamPayApplyMemberService.self().getByBusinessTradeNo(apiReq.getBody().getTradeNo());
                    if (applyMember != null && TeamPayApplyMemberEnum.State.Waiting.index().equals(applyMember.getState())) {
                        Goods goods = GoodsService.self().getById(applyMember.getGoodsId());

                        if (goods == null || GoodsEnum.State.OffTheShelf.index().equals(goods.getState())) {
                            //TODO 关闭支付
                            apiResp.setCode(ClientAPICodeConstants.EXECUTE_ERROR, "商品已下架");
                            return apiResp;
                        } else if (GoodsEnum.State.NoInventory.index().equals(goods.getState())) {
                            //TODO 关闭支付
                            apiResp.setCode(ClientAPICodeConstants.EXECUTE_ERROR, "商品暂无库存");
                            return apiResp;
                        }

                        MemberOrderWaitPayInfoResponse.GoodsEntity goodsEntity = new MemberOrderWaitPayInfoResponse.GoodsEntity();
                        goodsEntity.setGoodsId(goods.getId());
                        goodsEntity.setGoodsName(goods.getName());
                        goodsEntity.setThumbnailImg(goods.getThumbnailImg());
                        if (StringUtils.isNotBlank(goods.getThumbnailImg())) {
                            if (!goods.getThumbnailImg().startsWith("http")) {
                        		String resDomain = PropertyTools.get().getString("marketResDomain");
                                goodsEntity.setThumbnailImg(resDomain + goods.getThumbnailImg());
                            }
                        }
                        goodsEntity.setPayMoney(goods.getPayMoneyStr());
                        goodsEntity.setTeamPayMoney(goods.getTeamPayMoneyStr());
                        goodsEntity.setSupplierId(goods.getSupplierId());

                        MemberOrderWaitPayInfoResponse.MailAddress addressInfo = new MemberOrderWaitPayInfoResponse.MailAddress();
                        addressInfo.setId(order.getMailAddressId());
                        addressInfo.setReceiveName(order.getReceiveName());
                        addressInfo.setReceivePhone(order.getReceivePhone());
                        addressInfo.setAddress(order.getReceiveAddress());

                        apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                        apiResp.getBody().setTradeNo(applyMember.getBusinessTradeNo());
                        apiResp.getBody().setBusinessType(PayGoodsOrderEnum.BusinessType.TeamPay.index());
                        apiResp.getBody().setGoodsNum(applyMember.getGoodsCount());
                        apiResp.getBody().setSpecificationId(applyMember.getGoodsSpaceId());
                        apiResp.getBody().setSpecificationName(applyMember.getGoodsSpaceName());
                        apiResp.getBody().setApplyId(applyMember.getApplyId());
                        apiResp.getBody().setApplyState(applyMember.getState());
                        apiResp.getBody().setAddressInfo(addressInfo);
                        apiResp.getBody().setGoods(goodsEntity);
                        apiResp.getBody().setLogisticsCompanyId(applyMember.getLogisticsCompanyId());
                    }
                }
            }
        }
        return apiResp;
    }
	
	/**
     * 修改订单收货地址
     * @param request
     * @return
     */
    @Route(value = { "/member/order/address/update.do" })
    public DefaultResponse updateOrderAddress(HttpServletRequest request) {
        DefaultResponse apiResp = new DefaultResponse(ClientAPICodeConstants.EXECUTE_ERROR, "收货地址修改失败，请稍后再试");
        MemberOrderUpdateAddressRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderUpdateAddressRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getTradeNo()) && apiReq.getBody().getAddressId() != null) {
        	LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (loginMember != null) {
                PayGoodsOrder order = PayGoodsOrderService.self().getByTradeNo(apiReq.getBody().getTradeNo(), loginMember.getId());
                if (order != null) {
                    MemberAddress memberAddress = MemberAddressService.self(BaseConstants.JDBC_KEY).getByKeys(loginMember.getId(),apiReq.getBody().getAddressId());
                    if (memberAddress == null) {
                        apiResp.setMsg("未找到要修改的地址信息");
                        return apiResp;
                    }
                    try {
                        if (PayGoodsOrderService.self().updateOrderAddress(order, memberAddress)) {
                            apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        apiResp.setMsg(e.getMessage());
                    }
                } else {
                    TeamPayApplyMember applyMember = TeamPayApplyMemberService.self().getByPayTradeNo(apiReq.getBody().getTradeNo());
                    if (applyMember != null) {
                        apiResp.setMsg("暂不支持修改地址");
                    } else {
                        apiResp.setMsg("未找到订单信息，请重新登陆后再试");
                    }
                }
            }
        }
        return apiResp;
    }

    @Route(value = { "/member/order/close.do" })
    public DefaultAPIResponse closeOrder(HttpServletRequest request) {
        DefaultAPIResponse apiResp = new DefaultAPIResponse(ClientAPICodeConstants.EXECUTE_ERROR, "关闭订单失败，请稍后再试");
        MemberOrderCloseRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderCloseRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getTradeNo())) {
            if (StringTools.isBlank(apiReq.getBody().getCloseReason())) {
                apiResp.setMsg("请填写关闭原因");
                return apiResp;
            }

            LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (loginMember != null) {
                PayGoodsOrder order = PayGoodsOrderService.self().getByTradeNo(apiReq.getBody().getTradeNo(), loginMember.getId());
                if (order != null) {
                    try {
                        if (PayGoodsOrderService.self().closeOrder(order, apiReq.getBody().getCloseReason())) {
                            apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        apiResp.setMsg(e.getMessage());
                    }
                } else {
                    TeamPayApplyMember applyMember = TeamPayApplyMemberService.self().getByPayTradeNo(apiReq.getBody().getTradeNo());
                    if (applyMember != null) {
                        try {
                            if (TeamPayApplyMemberService.self().closeApply(applyMember, apiReq.getBody().getCloseReason())) {
                                apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                            }
                        } catch (LogicException e) {
                            apiResp.setMsg(e.getMessage());
                        }
                    }
                }
            }
        }
        return apiResp;
    }
	
	/**
     * 确定收货
     * @param request
     * @return
     */
    @Route(value = { "/member/order/goods/receive.do" })
    public DefaultAPIResponse receiveGoods(HttpServletRequest request) {
        DefaultAPIResponse apiResp = new DefaultAPIResponse(ClientAPICodeConstants.EXECUTE_ERROR, "确定收货失败，请稍后再试");
        MemberOrderReceiveGoodsRequest apiReq = WebUtils.getJsonRequest(request, MemberOrderReceiveGoodsRequest.class);
        if (apiReq != null && StringTools.isNotBlank(apiReq.getBody().getTradeNo())) {
        	LoginMember loginMember = WebUtils.getAttribute(request, MicroConstants.ATTR_LOGIN_MEMBER, LoginMember.class);
            if (loginMember != null) {
                PayGoodsOrder order = PayGoodsOrderService.self().getByTradeNo(apiReq.getBody().getTradeNo(), loginMember.getId());
                if (order != null) {
                    OperateInfo operateInfo = new OperateInfo();
                    operateInfo.setProgramType(MicroConstants.PROGRAM_TYPE);
                    operateInfo.setOperatorType(EOperatorType.Member.index());
                    operateInfo.setOperator(loginMember.getNickname());
                    try {
                        if (PayGoodsOrderService.self().receiveGoods(order, operateInfo)) {
                            apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        apiResp.setMsg(e.getMessage());
                    }
                }
            }
        }
        return apiResp;
    }
}
